define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-edit").data("area", ["100%", "100%"]);//编辑
            $(".btn-add").data("area", ["100%", "100%"]); //添加
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'offline/offline_information/index' + location.search,
                    add_url: 'offline/offline_information/add',
                    edit_url: 'offline/offline_information/edit',
                    del_url: 'offline/offline_information/del',
                    multi_url: 'offline/offline_information/multi',
                    import_url: 'offline/offline_information/batchImport',
                    table: 'offline_information',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('创建者昵称'), operate: 'LIKE'},
                        {field: 'users.nickname', title: __('被关联者昵称'), operate: 'LIKE'},
                        {
                            field: 'username',
                            title: __('Username'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'wxh',
                            title: __('Wxh'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'address',
                            title: __('Address'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'sex', title: __('性别'),
                            searchList: {1: __('男'), 2: __('女')},
                            formatter: Table.api.formatter.flag
                        },
                        {field: 'mobile', title: __('Mobile')},
                        {
                            field: 'id_card',
                            title: __('Id_card'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'opening_bank',
                            title: __('Opening_bank'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'bank_card',
                            title: __('Bank_card'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {
                            field: 'topic',
                            title: __('Topic'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'industry',
                            title: __('Industry'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'note',
                            title: __('Note'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'file', title: __('文件'), operate: false, formatter: Table.api.formatter.url},
                        {
                            field: 'filename',
                            title: __('附件名称'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'introduce',
                            title: __('Introduce'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'status', title: __('是否公开'),
                            searchList: {1: __('不公开'), 2: __('公开')},
                            formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'state', title: __('关联状态'),
                            searchList: {1: __('未关联'), 2: __('接受关联'), 3: __('拒绝关联')},
                            formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('线下资质列表'),
                                    title: __('线下资质列表'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    url: 'offline/offline_information/list',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            table.on('post-body.bs.table', function () {
                $(".btn-editone").data("area", ["100%", "100%"]);
                $(".btn-addone").data("area", ["100%", "100%"]);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).ready(function () {
                $('.btn-download').attr('href', '/excelTemplate/后台导入人脉资料Excel模板.xlsx');
            });
            $('.btn-update-userinfo').click(function () {
                var selectIds = Table.api.selectedids(table).join(',');
                $.ajax({
                    url: 'offline/offline_information/offlineUpdate',
                    type: 'POST',
                    data: {ids: selectIds},
                    dataType: 'json',
                    success: function(res) {
                        // 成功处理逻辑
                        if(res.code == 1) {
                            Toastr.success(res.msg);
                            setTimeout(function () {
                                window.location.reload();
                            }, 2000);
                            return ;
                        }
                        Toastr.error(res.msg);
                        return ;
                    },
                    error: function(res){
                        // 错误时处理逻辑
                        console.log(res);
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        qualifications: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'offline/offline_information/list' + location.search + '&ids=' + Fast.api.query('ids'),
                    add_url: 'offline/offline_information/qualifications?ids=' + Fast.api.query('ids'),
                    edit_url: 'offline/offline_information/updates',
                    del_url: 'offline/offline_information/deletes',
                    table: 'offline_information',
                }
            });
            var tables = $("#tables");
            // 初始化表格
            tables.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('资质名称')},
                        {field: 'path', title: __('资质路径'), operate: false, formatter: Table.api.formatter.url},
                        {field: 'path_file_names', title: __('上传的资质文件名称')},
                        {
                            field: 'status', title: __('资质状态'),
                            searchList: {1: __('待审核'), 2: __('审核通过'), 3: __('审核驳回')},
                            formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: tables,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(tables);
        },
        updates: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
